<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-input
          placeholder="请输入内容"
          v-model="queryParams.query"
          class="input-with-select"
          clearable
          @clear="handleSearch"
        >
          <el-button
            @click="handleSearch"
            slot="append"
            icon="el-icon-search"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="3">
        <el-button @click='addGoods' type="primary">添加商品</el-button>
      </el-col>
    </el-row>
    <el-table style="margin: 20px 0" :data="goodsList" border stripe>
      <el-table-column type="index" label="#"> </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="goods_name"
        label="商品名称"
        width="600"
      >
      </el-table-column>
      <el-table-column prop="goods_price" label="商品价格(元)">
      </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
      <el-table-column prop="add_time" label="创建时间" width="180">
        <template scope="scope">
          {{ scope.row.add_time | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column prop="date" width="200px" label="操作">
        <template scope="scope">
             <el-button size="mini" type="primary" @click="editFn(scope.row)"
            >编辑<i class="el-icon-edit"></i
          ></el-button>
          <el-button size="mini" type="danger" @click='delFn(scope.row.goods_id)'
            >删除<i class="el-icon-delete"></i
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pagenum"
      :page-sizes="[5, 10, 30, 40]"
      :page-size="queryParams.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="收货地址" :visible.sync="editShow">
  <el-form label-width="100px" ref="formRef" :rules="rules" :model="form">
    <el-form-item label="商品名称" prop="goods_name" >
      <el-input v-model="form.goods_name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="goods_price" >
      <el-input v-model.number="form.goods_price" autocomplete="off"></el-input>
    </el-form-item>
     <el-form-item label="数量" prop="goods_number" >
      <el-input v-model.number="form.goods_number" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="重量" prop="goods_weight" >
      <el-input v-model.number="form.goods_weight" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editShow = false">取 消</el-button>
    <el-button type="primary" @click="onEdit">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import { getGoodsList, delGoods, editGoods } from '../../api/goods';

export default {
  data() {
    return {
      editShow: false,
      goodsList: [],
      queryParams: {
        query: '',
        pagesize: 10,
        pagenum: 1,
      },
      total: 0,
      form: {
        goods_name: '',
        goods_price: '',
        goods_weight: '',
        goods_number: '',
      },
      editId: null,
      rules: {
        goods_name: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
        goods_price: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
        goods_weight: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
        goods_number: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    editFn(obj) {
      console.log(obj);
      this.editShow = true;
      this.form.goods_name = obj.goods_name;
      this.form.goods_price = obj.goods_price;
      this.form.goods_weight = obj.goods_weight;
      this.form.goods_number = obj.goods_number;
      this.editId = obj.goods_id;
    },
    onEdit() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return false;
        this.doEdit();
      });
    },
    async doEdit() {
      console.log(this.editId);
      const res = await editGoods(this.editId, this.form);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      this.getGoods();
    },
    // 添加商品
    addGoods() {
      this.$router.push('/goods/add');
    },
    // 删除
    delFn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await delGoods(id);
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
        this.$message.success(res.meta.msg);
        this.getGoods();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除',
        });
      });
    },
    handleSearch() {
      this.queryParams.pagenum = 1;
      this.getGoods();
    },
    handleSizeChange(size) {
      this.queryParams.pagesize = size;
      this.getGoods();
    },
    handleCurrentChange(num) {
      this.queryParams.pagenum = num;
      this.getGoods();
    },
    async getGoods() {
      const res = await getGoodsList(this.queryParams);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
  },
  created() {
    this.getGoods();
  },
};
</script>

<style>
</style>
